package com.platform.manager.mail;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * Created with IntelliJ IDEA.
 * User: wangyanginfo
 * Date: 12-8-28
 * Time: 下午2:13
 * To change this template use File | Settings | File Templates.
 */
@Service("sendMail")
public class SendMail {

    @Resource
    private MailSender mailSender;

    public void sendMessage() {
        try {
            MimeMessage message = ((JavaMailSenderImpl) mailSender).createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setTo("wangyanginfo@360buy.com");
            helper.setFrom("wangyanginfo@360buy.com");
            helper.setSubject("html+图片测试");
            helper.setText("<html><head></head><body><h1 style='color:red'>hello!!spring html Mail</h1></body></html>", true);
            //添加图片

            FileSystemResource file = new FileSystemResource(new File("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg"));
            helper.addAttachment("CoolImage.jpg", file);

            ((JavaMailSenderImpl) mailSender).send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        System.out.println("邮件发送成功..");
    }
}
